import 'package:flutter/foundation.dart';

import '../utils.dart';

enum OhosStreamUsage {
  unknown,
  music,
  voiceCommunication,
  voiceAssistant,
  alarm,
  voiceMessage,
  ringtone,
  notification,
  accessibility,
  movie,
  game,
  audioBook,
  navigation,
  videoCommunication,
}

extension OhosStreamUsageExt on OhosStreamUsage {
  String get value => describeEnum(this);
}

extension OhosStreamUsageEnumEx on String {
  OhosStreamUsage toOhosStreamUsage() => OhosStreamUsage.values
      .firstWhere((d) => describeEnum(d) == toLowerCase());
}

enum OhosSourceType {
  invalid,
  mic,
  voiceRecognition,
  voiceCommunication,
  voiceMessage,
  camcorder
}

extension OhosSourceTypeExt on OhosSourceType {
  String get value => describeEnum(this);
}

extension OhosSourceTypeEnumEx on String {
  OhosSourceType toOhosSourceType() =>
      OhosSourceType.values.firstWhere((d) => describeEnum(d) == toLowerCase());
}

class OhosAudioConfiguration {
  OhosAudioConfiguration({
    this.ohosStreamUsage,
    this.ohosSourceType,
  });

  final OhosStreamUsage? ohosStreamUsage;
  final OhosSourceType? ohosSourceType;

  Map<String, dynamic> toMap() => <String, dynamic>{
        if (ohosStreamUsage != null) 'ohosStreamUsage': ohosStreamUsage!.value,
        if (ohosSourceType != null) 'ohosSourceType': ohosSourceType!.value,
      };

  /// A pre-configured OhosAudioConfiguration for media playback.
  static final media = OhosAudioConfiguration(
    ohosStreamUsage: OhosStreamUsage.music,
    ohosSourceType: OhosSourceType.camcorder,
  );

  /// A pre-configured OhosAudioConfiguration for voice communication.
  static final communication = OhosAudioConfiguration(
    ohosStreamUsage: OhosStreamUsage.voiceCommunication,
    ohosSourceType: OhosSourceType.mic,
  );
}

class OhosNativeAudioManagement {
  static Future<void> setOhosAudioConfiguration(
      OhosAudioConfiguration config) async {
    if (WebRTC.platformIsOhos) {
      await WebRTC.invokeMethod(
        'setOhosAudioConfiguration',
        <String, dynamic>{'configuration': config.toMap()},
      );
    }
  }
}
